<?php

namespace Apps\Manage;


use Apps\Common\Plugin\NotFoundPlugin;
use Apps\Common\Plugin\BackendSecurityPlugin;
use Phalcon\Mvc\Dispatcher;
use PhalconPlus\Common\BaseModule;
use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Events\Manager as EventsManager;


class Module extends BaseModule implements ModuleDefinitionInterface
{
    const DIR = __DIR__;

    /**
     * Registers the module-only services
     * @param \Phalcon\DiInterface $di
     */
    public function registerServices(\Phalcon\DiInterface $di)
    {

        parent::registerServices($di);

        $di->set('tagNav',  function() {
            return new \PhalconPlus\Html\TagNav();
        });

        $di->set('dispatcher', $this->initDispatcher());

    }

    public function initDispatcher()
    {
        $eventsManager = new EventsManager;

        /**
         * Check if the user is allowed to access certain action using the BackendSecurityPlugin
         */
        $eventsManager->attach('dispatch:beforeDispatch', new BackendSecurityPlugin);

        /**
         * Handle exceptions and not-found exceptions using NotFoundPlugin
         */
        $eventsManager->attach('dispatch:beforeException', new NotFoundPlugin);

        $dispatcher = new Dispatcher;
        $dispatcher->setEventsManager($eventsManager);

        return $dispatcher;
    }

    public function getVoltCompileFunction($di)
    {
        return include __DIR__ . '/config/voltFunction.php';
    }
}
